//
//  ViewController.swift
//  OT Statistic
//
//  Created by Nguyen Van Truong on 1/3/15.
//  Copyright (c) 2015 Nguyen Van Truong. All rights reserved.
//

import UIKit
import CoreData

class ViewController: UIViewController {
    @IBOutlet weak var txttime: UITextField!
    @IBOutlet weak var txtdate: UITextField!
    @IBOutlet weak var btnUpdate: UIButton!
    @IBAction func btnUpdate(sender: UIButton) {
        if txtdate.text == "" || txttime.text == ""
        {
            let alert = UIAlertController(title: "Thông báo", message: "Thời gian và ngày giờ không được rỗng!\nHãy nhập lại!", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
            return
        }
        //Khai bao CSDL
        var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate);
        var context:NSManagedObjectContext = appDel.managedObjectContext!;
        
        //Tao doi tuong muon luu
        var newInfo = NSEntityDescription.insertNewObjectForEntityForName("ThongTin", inManagedObjectContext: context) as NSManagedObject;
        //Thiet lap gia tri cho doi tuong muon luu
        newInfo.setValue(""+txttime.text, forKey: "txttime");
        newInfo.setValue(""+txtdate.text, forKey: "txtdate");
        //Luu du lieu
        context.save(nil);
        //alert save ok
        let alert = UIAlertController(title: "Thông báo", message: "Bạn đã save thành công!", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
        // reset gia tri textbox
        txtdate.text = ""
        txttime.text = ""
    }
    
    @IBAction func btnShow(sender: AnyObject) {
        //Khai bao CSDL
        var appDel:AppDelegate = (UIApplication.sharedApplication().delegate as AppDelegate);
        var context:NSManagedObjectContext = appDel.managedObjectContext!;
        
        //Khoi tao Request
        var request=NSFetchRequest(entityName:"ThongTin");
        request.returnsObjectsAsFaults = false;
        request.predicate = NSPredicate(format: "txtdate = %@","" + txtdate.text);
        //Lay ket qua tra ve
        var results:NSArray = context.executeFetchRequest(request, error: nil)!;
        //Xu ly ket qua tra ve
        if(results.count>0){
            var res = results[0] as NSManagedObject;
            txttime.text = res.valueForKey("txttime") as? String;
        } else {
            txttime.text = "Không có dữ liệu";
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    override func touchesBegan(touches: NSSet, withEvent event: (UIEvent!)) {
        self.view.endEditing(true)
    }
}

